<!-- src/views/login/index.vue -->
<template>
  <div class="admin-login-page">
    <div class="login-container">
      <div class="login-card">
        <div class="login-header">
          <h1>{{ appTitle }}</h1>
          <p>管理员登录入口</p>
        </div>
        
        <el-form 
          :model="form" 
          :rules="rules" 
          ref="loginForm" 
          class="login-form"
          @submit.prevent="handleLogin"
        >
          <el-form-item prop="username">
            <el-input
              v-model="form.username"
              placeholder="管理员账号"
              size="large"
              :prefix-icon="User"
              @keyup.enter="handleLogin"
            />
          </el-form-item>
          
          <el-form-item prop="password">
            <el-input
              v-model="form.password"
              type="password"
              placeholder="管理员密码"
              size="large"
              :prefix-icon="Lock"
              show-password
              @keyup.enter="handleLogin"
            />
          </el-form-item>
          
          <el-form-item>
            <el-button 
              type="primary" 
              size="large" 
              class="login-btn"
              :loading="loading"
              @click="handleLogin"
            >
              {{ loading ? '登录中...' : '管理员登录' }}
            </el-button>
          </el-form-item>
        </el-form>
        
        <div class="login-footer">
          <p>测试账号: admin / 123456</p>
          <p class="version">版本: {{ appVersion }}</p>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { User, Lock } from '@element-plus/icons-vue'
import { useUserStore } from '@/store/modules/user'  
import { useAppStore } from '@/store/modules/app'
import { authAPI } from '@/api'

const router = useRouter()
const userStore = useUserStore()
const appStore = useAppStore()
const loginForm = ref()
const loading = ref(false)

const appTitle = import.meta.env.VITE_APP_TITLE || 'Easy Go 后台管理系统'
const appVersion = import.meta.env.VITE_APP_VERSION || '1.0.0'

const form = reactive({
  username: '',
  password: ''
})

const rules = reactive({
  username: [
    { required: true, message: '请输入管理员账号', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, message: '密码长度至少6位', trigger: 'blur' }
  ]
})

const handleLogin = async () => {
  if (!loginForm.value) return
  
  try {
    await loginForm.value.validate()
  } catch (error) {
    return
  }
  
  loading.value = true
  appStore.setPageLoading(true)
  
  try {
    console.log('管理员登录请求:', form.username)
    
    // 使用真实 API 登录
    const response = await authAPI.login({
      username: form.username,
      password: form.password
    })
    
    // 保存用户信息和 token
    userStore.setUser(response.user)
    userStore.setToken(response.token)
    
    ElMessage.success('管理员登录成功！')
    console.log('登录状态:', userStore.hasToken)
    
    // 跳转到 dashboard
    router.push('/dashboard')
    
  } catch (error) {
    console.error('登录错误:', error)
    // 错误信息已经在拦截器中统一处理了
  } finally {
    loading.value = false
    appStore.setPageLoading(false)
  }
}

// 如果已经登录，自动跳转到首页
onMounted(() => {
  if (userStore.hasToken) {
    router.push('/dashboard')
  }
})
</script>

<style scoped>
.admin-login-page {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 20px;
}

.login-container {
  width: 100%;
  max-width: 400px;
}

.login-card {
  background: white;
  padding: 2.5rem;
  border-radius: 12px;
  box-shadow: 0 15px 35px rgba(0, 0, 0, 0.1);
}

.login-header {
  text-align: center;
  margin-bottom: 2rem;
}

.login-header h1 {
  color: #2c3e50;
  margin-bottom: 0.5rem;
  font-size: 1.5rem;
  font-weight: 600;
}

.login-header p {
  color: #7f8c8d;
  margin: 0;
  font-size: 0.9rem;
}

.login-form {
  margin-bottom: 1.5rem;
}

.login-btn {
  width: 100%;
  height: 45px;
  font-size: 16px;
}

.login-footer {
  text-align: center;
  padding-top: 1.5rem;
  border-top: 1px solid #ecf0f1;
}

.login-footer p {
  color: #95a5a6;
  font-size: 0.8rem;
  margin: 0;
}

.login-footer .version {
  margin-top: 0.5rem;
  font-size: 0.7rem;
  color: #bdc3c7;
}
</style>